from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import StaleElementReferenceException
import schedule
import time

# 定义函数，用于执行点击操作
def perform_click_action(link_class_hot, hot_title, link_class_live, live_text, click_count, username, password):
    chromedriver_path = r'C:\Users\luffy\PycharmProjects\autoclick\chromedriver.exe'
    service = ChromeService(executable_path=chromedriver_path)
    driver = webdriver.Chrome(service=service)
    driver.implicitly_wait(10)

    driver.get("https://www.bilibili.com/")
    driver.maximize_window()
    time.sleep(3)

    # 点击登录按钮
    login_button = driver.find_element(By.CLASS_NAME, "header-login-entry")
    login_button.click()

    # 输入账号和密码
    login_username = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, 'input[placeholder="请输入账号"]'))
    )
    login_username.send_keys(username)
    login_password = driver.find_element(By.CSS_SELECTOR, 'input[placeholder="请输入密码"]')
    login_password.send_keys(password)
    # 点击内部登录按钮
    login_button = driver.find_element(By.CLASS_NAME, "btn_primary")
    login_button.click()
    time.sleep(1)
    # 点击关闭验证码按钮
    login_button = driver.find_element(By.CLASS_NAME, "geetest_close")
    login_button.click()

    time.sleep(1)
    login_button = driver.find_element(By.CLASS_NAME, "bili-mini-close-icon")
    login_button.click()

    # 等待5秒
    time.sleep(1)

    # 交替点击热门标签和直播链接
    for _ in range(click_count):
        click_hot_link(driver, link_class_hot, hot_title)
        # 等待5秒
        time.sleep(1)
        click_live_link(driver, link_class_live, live_text)
        # 等待5秒
        time.sleep(1)

    driver.quit()

# 定义函数，用于点击热门标签
def click_hot_link(driver, link_class, link_text):
    link_elements = WebDriverWait(driver, 10).until(
        EC.presence_of_all_elements_located((By.CLASS_NAME, link_class))
    )

    for link_element in link_elements:
        if link_text in link_element.text:
            try:
                link_element.click()
                time.sleep(1)
            except StaleElementReferenceException:
                pass

# 定义函数，用于点击直播链接
def click_live_link(driver, link_class, link_text):
    link_elements = WebDriverWait(driver, 10).until(
        EC.presence_of_all_elements_located((By.CLASS_NAME, link_class))
    )

    for link_element in link_elements:
        if link_text in link_element.text:
            try:
                link_element.click()
                time.sleep(1)
            except StaleElementReferenceException:
                pass

# 定义任务函数
def job():
    link_class_hot = "channel-icons__item"  # 热门标签的 class 属性
    hot_title = "热门"  # 热门标签的文本
    link_class_live = "default-entry"  # 直播链接的 class 属性
    live_text = "直播"  # 直播链接的文本
    click_count = 1

    # 替换为你自己的账号和密码
    your_username = "LY酱"
    your_password = "XLPjz963852"

    # 调用执行点击操作的函数
    perform_click_action(link_class_hot, hot_title, link_class_live, live_text, click_count, your_username, your_password)

# 每天的特定时间执行任务
schedule.every().day.at("00:02").do(job)

# 立即执行任务
job()

# 每五分钟循环执行任务
# schedule.every(15).seconds.do(job)

# 进入循环，持续运行任务
while True:
    schedule.run_pending()
    time.sleep(1)
